package com.hctg.forced2sleep.activity;

import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;

import com.actor.myandroidframework.utils.LogUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.hctg.forced2sleep.R;
import com.hctg.forced2sleep.databinding.ActivityTestBinding;
import com.hctg.forced2sleep.dialog.Tips2BtnDialog;
import com.hctg.forced2sleep.utils.UsageStatsUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * description: 描述
 * company    :
 * @author    : ldf
 * date       : 2024/7/9 on 14:47
 */
public class TestActivity extends BaseActivity<ActivityTestBinding> {

    public static void start(Context context) {
        context.startActivity(new Intent(context, TestActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewClicked(@NonNull View view) {
        switch (view.getId()) {
            case R.id.btn0:
                List<UsageStats> todayUsage = UsageStatsUtils.getTodayUsage();
                if (todayUsage != null) {
                    for (UsageStats usageStats : todayUsage) {
                        String packageName = usageStats.getPackageName();
                        LogUtils.errorFormat("%s: %s", packageName, GsonUtils.toJson(usageStats));
                    }
                }
                break;
            case R.id.btn1:
                long now = System.currentTimeMillis();
                List<UsageStats> usageStats = UsageStatsUtils.queryUsage(UsageStatsManager.INTERVAL_DAILY, now - 10_000L, now);
                if (usageStats == null || usageStats.isEmpty()) {
                    LogUtils.errorFormat("usageStats == null: %b", usageStats == null);
                } else {
                    for (UsageStats usageStat : usageStats) {
                        String packageName = usageStat.getPackageName();
                        LogUtils.errorFormat("%s: %s", packageName, GsonUtils.toJson(usageStats));
                    }
                }
                break;
            case R.id.btn2:
                ThreadUtils.executeBySingleAtFixRate(new ThreadUtils.SimpleTask<Void>() {
                    @Override
                    public Void doInBackground() throws Throwable {
                        ComponentName componentName = UsageStatsUtils.getProcessName();
                        LogUtils.error(componentName);
                        LogUtils.error("=============================================");
                        return null;
                    }
                    @Override
                    public void onSuccess(Void result) {
                    }
                }, 1L, TimeUnit.SECONDS);
                break;
            case R.id.btn3:
                UsageStatsUtils.queryEventsForSelf();
                break;
            case R.id.btn_page_find://页面框选全部元素
                new Tips2BtnDialog(mActivity, "标题", "内容啊大是大非是否水电费内容啊大是大非是否水电费", v -> {
                }).show();
                break;
            case R.id.btn_lock_screen:  //锁屏方案测试
//            int screenHeight = ScreenUtils.getScreenHeight();       //1440, 包括状态栏 & 导航栏
//            int appScreenHeight = ScreenUtils.getAppScreenHeight(); //1358, 包括状态栏
//            int statusBarHeight = BarUtils.getStatusBarHeight();    //48
//            int navBarHeight = BarUtils.getNavBarHeight();          //82
//            int actionBarHeight = BarUtils.getActionBarHeight();    //112
//            LogUtils.errorFormat("screenHeight:%d, appScreenHeight:%d, statusBarHeight:%d, navBarHeight:%d, actionBarHeight:%d, ",
//                    screenHeight, appScreenHeight, statusBarHeight, navBarHeight, actionBarHeight);

                startActivity(new Intent(mActivity, LockUnLockScreenActivity.class));
                break;
            default:
                break;
        }
    }
}